package com.qcby.admin.service.impl;

import com.alibaba.fastjson.JSON;
import com.qcby.admin.utils.WebUtils;
import com.qcby.framwork.utils.AppHttpCodeEnum;
import com.qcby.framwork.utils.ResponseResult;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException,
            ServletException {
        accessDeniedException.printStackTrace();
        //未授权
        ResponseResult result = ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH);
//响应给前端
        WebUtils.renderString(response, JSON.toJSONString(result));
    }
}
